const app = getApp();
const { pickerlist: provinceList } = require('../../../common/utils/util');
const api = require('../../../../ws_micro_shop/utils/api.js');

Page({
  data: {
    getMore: false,
    albumCount: 1,
    gender: -1,
    avatar: '',
    radio: [{
      name: '男',
      value: 1,
      checked: false,
    }, {
      name: '女',
      value: 2,
      checked: false,
    }],
    provinceList,
    birthday: '',
    province: '',
  },
  onLoad(opts) {

    this.setData({
      type: opts.type,
      opts,
    });
    opts.type === 'edit' && this.getInfo(opts.id);
  },

  getInfo(id) {
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.staff.clue_user_detail,
      data: {
        customer_id: id,
      },
      success: (res) => {
        wx.hideLoading();
        if (res.code === 0) {
          let { address = '', age = '', birthday = '', company_name = '', company_position = '', gender = '', mail = '', mobile = '', nickname = '', province = '', qq_id = '', realname = '', remark = '', user_id = '', wechat_id = '', wechat_nickname = '' } = res.data;
          if (Number(gender) === 1) {
            this.setData({
              'radio[0].checked': true,
            });
          } else if (Number(gender) === 2) {
            this.setData({
              'radio[1].checked': true,
            });
          }
          this.setData({
            address, age, birthday, company_name, company_position, gender, mail, mobile, nickname, province, qq_id, realname, remark, user_id, wechat_id, wechat_nickname,
          });

        }
      },
    });
  },

  formSubmit(e) {
    let data = e.detail.value;
    let { type, avatar, opts, birthday, province } = this.data;
    if (type === 'edit') Object.assign(data, opts);
    // 检查用户输入的手机号
    let exp = app.util.phoneExp.mobile;
    if (data.mobile && !exp.test(data.mobile)) {
      wx.showToast({
        title: '请输入有效的手机号',
        icon: 'none',
        mask: true,
        duration: 1500,
      });
      return;
    }
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (data.mail && !myreg.test(data.mail)) {
      wx.showToast({
        title: '邮箱格式不正确',
        icon: 'none',
        mask: true,
        duration: 1500,
      });
      return;
    }
    data.birthday = birthday;
    data.province = province;
    data.headimg = avatar;
    data.customer_id = this.data.opts.id;
    app.ws_micro_shoprequest({
      url: api.staff.clue_user_detail,
      data,
      method: 'post',
      success: (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            success: res => {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500);
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            mask: true,
            duration: 1500,
          });
        }
      },
    });
  },


  birthdayChange(e) {
    this.setData({
      birthday: e.detail.value,
    });
  },
  provinceChange(e) {
    let location = e.detail.value;
    let province;
    if (location[0].slice(-1) === '市') {
      province = location[0].slice(0, -1);
    } else if (location[0] === '新疆维吾尔自治区' || location[0] === '广西壮族自治区' || location[0] === '西藏自治区' || location[0] === '宁夏回族自治区' || location[0] === '新疆维吾尔自治区') {
      if (location[1].slice(-1) === '市' || location[1].slice(-1) === '县') {
        province = location[0].slice(0, 2) + ' ' + location[1].slice(0, -1);
      } else {
        province = location[0].slice(0, 2) + ' ' + location[1];
      }
    } else if (location[0] === '内蒙古自治区') {
      if (location[1].slice(-1) === '市' || location[1].slice(-1) === '县') {
        province = location[0].slice(0, 3) + ' ' + location[1].slice(0, -1);
      } else {
        province = location[0].slice(0, 3) + ' ' + location[1];

      }
    } else if (location[0].slice(-5) === '特别行政区') {
      province = location[0].slice(0, 2);
    } else {
      if (location[1].slice(-1) === '市' || location[1].slice(-1) === '县') {
        province = location[0].slice(0, -1) + ' ' + location[1].slice(0, -1);
      } else {
        province = location[0].slice(0, -1) + ' ' + location[1];
      }
    }
    this.setData({
      province: province,
    });
  },
  getMore() {
    this.setData({
      getMore: true,
    });
  },
  radioChange(e) {
    this.setData({
      gender: e.detail.value,
    });
  },
});
